﻿using System;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using System.Web.UI.WebControls;
using BusinessLogic.Model;
using Mvp.NewsItems;
using BusinessLogic;
using BusinessLogic.StateManagers;
using BusinessLogic.Model.Repository;

namespace WebControls
{
    public partial class ViewNewsItem : BasePage, IViewNewsItemView
    {
        #region IViewNewsItemView Members

        private int newsItemID;
        public int NewsItemID
        {
            get { return newsItemID; }
        }
        public NewsItem NewsItem { get; set; }
        public INewsItemControlView NewsItemControl
        {
            get { return niCurrent; }
        }

        public int CommentsPageSize { get; set; }
        public bool CommentsEnabled { get; set; }
        public string CommentsStatusText { get; set; }

        public string NewCommentText
        {
            get { return txtCommentText.Text; }
        }

        public string GuestUsername
        {
            get { return ((TextBox)lvUsername.FindControl("txtNickname")).Text; }
        }

        public void ValidateCaptcha()
        {
            InvisibleCaptcha.IsCaptchaValid(this, ViewState, true);
        }

        #endregion

        private ViewNewsItemPresenter Presenter { get; set; }

        #region PageEvents

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (!Int32.TryParse(Request.QueryString[QueryStringConstants.NewsItemID], out newsItemID))
            {
                Session[Constants.Session.ErrorMessage] = Constants.Error.InvalidQueryString;
                Server.Transfer("~/Error.aspx");
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            Presenter = new ViewNewsItemPresenter(this);
            Presenter.Init();

            niCurrent.NewsItem = NewsItem;
            this.Title = NewsItem.Title;

            //dpCommentsLower.PageSize = CommentsPageSize;
            //dpCommentsUpper.PageSize = CommentsPageSize;

            //for some bizarre reason the DP does not take into consideration the query string argument
            //if I set the page size programatically but declare the query string field in markup
            //dpCommentsUpper.QueryStringField = QueryStringConstants.Page;
            //dpCommentsLower.QueryStringField = QueryStringConstants.Page;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                InvisibleCaptcha.InititializeCaptcha(this, ViewState);
            }

            if (!IsPostBack)
            {
                ViewState[Constants.ViewState.LastCommentID] = NewsItem.LastCommentID;
            }

            //hide pager if there are no comments
            if (NewsItem.CommentsCount <= 0)
            {
                dpCommentsLower.Visible = false;
                dpCommentsUpper.Visible = false;
            }
            else
            {
                dpCommentsLower.Visible = true;
                dpCommentsUpper.Visible = true;
            }

            Presenter.Load();

            pnlCommentsDisabled.Visible = !CommentsEnabled;
            pnlCommentsEnabled.Visible = CommentsEnabled;
            lblCommentsStatus.Text = CommentsStatusText;
        }

        #endregion

        #region ControlEvents

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Checks whether the last comment has changed. (it has been deleted or new one has been posted)
            if ((int?)ViewState[Constants.ViewState.LastCommentID] != NewsItem.LastCommentID)
            {
                commentSubmitError.Visible = true;
                ViewState[Constants.ViewState.LastCommentID] = NewsItem.LastCommentID;
                return;
            }

            if (!Page.IsValid)
            {
                return;
            }

            Presenter.SaveComment();
        }


        protected void odsComments_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = this.Presenter;
        }


        protected void ldsComments_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            
        }

        protected void ldsComments_Deleted(object sender, LinqDataSourceStatusEventArgs e)
        {
            
        }

        #endregion
    }
}